package com.xframework.gateway.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

/**
 * Created by xuning on 2020/8/22.
 */
//@Service
public class DynamicRouteService {
//    public class DynamicRouteService implements ApplicationEventPublisherAware {

//    protected Logger logger = LoggerFactory.getLogger(this.getClass());
//
//    @Autowired
//    private RouteDefinitionWriter routeDefinitionWriter;
//    /**
//     * 发布事件
//     */
//    @Autowired
//    private ApplicationEventPublisher publisher;
//
//    @Override
//    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
//        this.publisher = applicationEventPublisher;
//    }
//
//    /**
//     * 增加路由
//     *
//     * @param definition
//     * @return
//     */
//    public String add(RouteDefinition definition) {
//        logger.info("gateway add route {}", definition);
//        routeDefinitionWriter.save(Mono.just(definition)).subscribe();
//        this.publisher.publishEvent(new RefreshRoutesEvent(this));
//        return "success";
//    }
//
//    /**
//     * 更新路由
//     *
//     * @param definition
//     * @return
//     */
//    public String update(RouteDefinition definition) {
//        try {
//            logger.info("gateway update route {}", definition);
//            this.routeDefinitionWriter.delete(Mono.just(definition.getId()));
//        } catch (Exception e) {
//            return "update fail,not find route  routeId: " + definition.getId();
//        }
//        try {
//            routeDefinitionWriter.save(Mono.just(definition)).subscribe();
//            this.publisher.publishEvent(new RefreshRoutesEvent(this));
//            return "success";
//        } catch (Exception e) {
//            return "update route fail";
//        }
//    }
//
//    /**
//     * 删除路由
//     *
//     * @param id
//     * @return
//     */
//    public String delete(String id) {
//        try {
//            logger.info("gateway delete route id {}", id);
//            this.routeDefinitionWriter.delete(Mono.just(id));
//            return "delete success";
//        } catch (Exception e) {
//            return "delete fail";
//        }
//    }

}
